// This class searches for script assemblies in scripts directory & then loads the scripts it finds

#pragma once
#include <windows.h>
#include "ScriptBase.h"
#include <list>

using namespace System::Reflection;

namespace GTA
{
	public ref class ScriptManager : MarshalByRefObject
	{
	private:
		static ScriptManager^ _Instance;
	public:
		ScriptManager::ScriptManager();
		static ScriptManager^ GetInstance() { return _Instance; };

		virtual Object^ ScriptManager::InitializeLifetimeService() override;
		//static ScriptManager^ GetInstance(AppDomain^ domain);

		void ScriptManager::Init(String^ coreAsmLocation); // Initialize environment variables
		int ScriptManager::FindScriptAssemblies(); // search the scripts directory for files matching scriptfilepattern, then send them to FindScriptsInAssembly()
		int FindScriptsInAssembly( String^ assemblypath ); // search specific file for scripts (children of ScriptBase class), then send them to RegisterScript()
		ScriptBase^ RegisterScript(Type^ scriptclass); // registers & starts a script

		static String^ coreAsmLocation; // location of GTAScript main assembly
		String^ ScriptManager::GameDirectory; // directory that coreasm is loaded from (eg. c:\Games\GTA IV)
		String^ ScriptManager::ScriptFileDirectory; // directory to search in for scripts (/scripts)
		String^ ScriptManager::ScriptFilePattern; // filename patter to search for scripts (*.Net.dll)
		System::Collections::Generic::List<ScriptBase^>^ runningScripts; // list of our running scripts

		void ScriptManager::OnStart();
		void ScriptManager::CallEvents();

		void ScriptManager::ReloadAllScripts();
		void ScriptManager::UnloadAllScripts();
		void ScriptManager::UnloadScript(ScriptBase^ script);

		void ScriptManager::InitalizeHooks(System::IntPtr DllHandle,System::UInt32 DllThreadId);
		void ScriptManager::UnloadHooks();


		static void currentDomain_DomainUnload(System::Object^ sender, EventArgs^ e );
		static Assembly^ currentDomain_AssemblyResolve( System::Object^ sender, ResolveEventArgs^ args ); // Resolve main assembly (for scripts ro find main assembly)
		static void currentDomain_UnhandledException( System::Object^ sender, UnhandledExceptionEventArgs^ e );


	};
};